package net.flintx.array;

/**
 * Created by flintx on 2018/1/10.
 */
public class MajorityElement {
    public int majorityElement(int[] nums) {
        int count, element = 0, index = 0;
        while (index < nums.length) {
            element = nums[index];
            count = 1;
            while (index < nums.length - 1 && count > 0) {
                if (nums[++index] == element)
                    count++;
                else count--;
            }
            if (index == nums.length - 1) {
                break;
            } else {
                index += 1;
            }
        }
        count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == element)
                count++;
        }
        if (count * 2 >= nums.length)
            return element;
        else
            return 0;
    }

    public static void main(String[] args) {
        int[] nums = {6,5,5};
        System.out.println(new MajorityElement().majorityElement(nums));
    }
}
